package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.wanmait.employment.entity.Admin;
import com.wanmait.employment.entity.Log;
import com.wanmait.employment.mapper.AdminMapper;
import com.wanmait.employment.mapper.LogMapper;
import com.wanmait.employment.service.LogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {


    @Resource
    private LogMapper logMapper;
    @Resource
    private AdminMapper adminMapper;
    @Override
    public PageInfo<Log> findAll(String name, Integer pageNum, Integer pageSize, Date date) {
        PageHelper.startPage(pageNum,pageSize);
        LambdaQueryWrapper<Log> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(date!=null){
            lambdaQueryWrapper.ge(Log::getInputTime,date);
        }
        lambdaQueryWrapper.inSql(Log::getAdminId,"select id from admin where name like '%"+name+"%'");
        List<Log> list=logMapper.selectList(lambdaQueryWrapper);
        list.forEach(log1 -> {
            LambdaQueryWrapper<Admin> adminLambdaQueryWrapper=new LambdaQueryWrapper<>();
            adminLambdaQueryWrapper.eq(Admin::getId,log1.getAdminId());
            Admin admin=adminMapper.selectOne(adminLambdaQueryWrapper);
            log1.setAdmin(admin);
        });
        PageInfo<Log> logPageInfo=new PageInfo<>(list);
        return logPageInfo;
    }
}
